/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using NMock2;
using NUnit.Framework;
using OpenServers.Net.Common.FileSystem;

namespace OpenServers.Net.FtpServer.Command {
    [TestFixture]
    public class MakeDirectoryCommandTest : AbstractCommandTest<MakeDirectoryCommand> {
        [Test]
        public void TestInvoke() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            IDirectory dirParent = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(false));
            Expect.AtLeastOnce.On(dir).GetProperty("Parent").Will(Return.Value(dirParent));
            Expect.Once.On(dirParent).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/test").Will(Return.Value(dir));
            Expect.Once.On(MockSecurityProvider).Method("CanWrite").With(MockUser, dirParent).Will(Return.Value(true));
            Expect.Once.On(dir).Method("Create");
            Expect.Once.On(MockMessages).Method("Get257").With("/test", Reason257.DirectoryCreated);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "test");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeAccessDenied() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            IDirectory dirParent = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(false));
            Expect.AtLeastOnce.On(dir).GetProperty("Parent").Will(Return.Value(dirParent));
            Expect.Once.On(dirParent).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/test").Will(Return.Value(dir));
            Expect.Once.On(MockSecurityProvider).Method("CanWrite").With(MockUser, dirParent).Will(Return.Value(false));
            Expect.Once.On(MockMessages).Method("Get550").With(Reason550.AccessDenied);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "test");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeAlreadyExists() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(true));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/test").Will(Return.Value(dir));
            Expect.Once.On(MockMessages).Method("Get550").With(Reason550.FileAlreadyExists);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "test");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeParentDoesntExist() {
            IDirectory dir = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dir).GetProperty("Exists").Will(Return.Value(false));
            IDirectory dirParent = Mocks.NewMock<IDirectory>();
            Expect.Once.On(dirParent).GetProperty("Exists").Will(Return.Value(false));
            Expect.Once.On(dir).GetProperty("Parent").Will(Return.Value(dirParent));
            Expect.Once.On(MockFileSystem).Method("GetDirectory").With("/test").Will(Return.Value(dir));
            Expect.Once.On(MockMessages).Method("Get550").With(Reason550.FileNotFound);

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "test");

            Assert.AreEqual("/", State.CurrentDirectory);
        }

        [Test]
        public void TestInvokeNotLoggedIn() {
            State.User = null;
            Expect.Once.On(MockMessages).Method("Get530");
            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "/");
        }

        [Test]
        public void TestInvokeBadArgument() {
            Expect.Once.On(MockMessages).Method("Get501");

            PrivateObject.WrappedObject.Invoke(MockFtpServer, State, "");

            Assert.AreEqual("/", State.CurrentDirectory);
        }
    }
}
